  
<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">订单管理</a>
    <a><cite>商城订单列表</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-userfront-formlist">
      <div class="layui-form-item">
        <div class="layui-inline">
          <!-- <label class="layui-form-label">消费类型</label> -->
          <div class="layui-input-block" hidden>
            <select name="gtype" id="" lay-filter="myselect" lay-verify="gtype" >
              <option value="">请选择</option>
              <option value="2">升级套餐</option>
              <option value="1" selected>普通商品</option>
           </select>
            <!-- <input type="text" name="gtype" placeholder="请输入" autocomplete="off" class="layui-input"> -->
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">订单状态</label>
          <div class="layui-input-block">
            <select name="status" id="" lay-filter="myselect" lay-verify="status" >
              <option value="">请选择</option>
              <option value="0">未支付</option>
              <option value="1">已支付</option>
              <option value="2">待收货</option>
              <option value="3">已确认收货</option>
           </select>
            <!-- <input type="text" name="status" placeholder="请输入" autocomplete="off" class="layui-input"> -->
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">用户名</label>
          <div class="layui-input-block">
            <input type="text" name="account" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-inline">
          <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-user-front-search">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
          </button>
        </div>
      </div>
    </div>
    
    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <!-- <button class="layui-btn layuiadmin-btn-useradmin" data-type="batchdel">删除</button> -->
        <!-- <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加</button> -->
        <button class="layui-btn" id="export">
          <i class="iconfont icon-export"></i> 导出
      </button>
      </div>
      
      <table id="LAY-order-manage" lay-filter="LAY-order-manage"></table>

      <script type="text/html" id="imgTpl"> 
        <img style="display: inline-block; width: 50%; height: 100%;" src= "{{ d.mainPicture }}"/>
      </script> 
      <script type="text/html" id="table-useradmin-webuser">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="orderInfo">详情</a>
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="logistics">发货</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="cacelOrder">取消订单</a>
      </script>
    </div>
  </div>
</div>

<script type="text/javascript">
  function createTime(v){
    var date = new Date(v);
      var y = date.getFullYear();
      var m = date.getMonth()+1;
      m = m<10?'0'+m:m;
      var d = date.getDate();
      d = d<10?("0"+d):d;
      var h = date.getHours();
      h = h<10?("0"+h):h;
      var M = date.getMinutes();
      M = M<10?("0"+M):M;
      var str = y+"-"+m+"-"+d+" "+h+":"+M;
      return str;
  }
</script>
<script>
layui.use('order', layui.factory('order')).use(['admin', 'order', 'table'], function(){
  var $ = layui.$
  ,admin = layui.admin
  ,view = layui.view
  ,table = layui.table
  ,setter = layui.setter
  ,form = layui.form;
  
  form.render(null, 'layadmin-userfront-formlist');
  
  //监听搜索
  form.on('submit(LAY-user-front-search)', function(data){
    var field = data.field;
    
    //执行重载
    table.reload('LAY-order-manage', {
      elem: '#LAY-order-manage'
      ,url: setter.myurl + 'admin/order/findList' //模拟接口
      ,cols: [[
        {type: 'checkbox', fixed: 'left'}
        ,{field: 'serialNum', width: 100, title: '订单号'}
        // ,{field: 'gtype', title: '消费类型'}
        ,{field: 'goodsName', title: '名称', width: 100}
        ,{field: 'disPrice', title: '优惠价'}
        ,{field: 'price', title: '原价'}
        ,{field: 'numb', title: '数量'}
        ,{field: 'totalPrice', title: '总价'}
        ,{field: 'account', title: '用户名'}
        // ,{field: 'mainPicture', title: '主图', id: 'imgTpl'}
        ,{field: 'status', title: '订单状态'}
        ,{field: 'consignee', title: '收货人姓名'}
        ,{field: 'cityProv', title: '省市'}
        ,{field: 'address', title: '收货地址'}
        ,{field: 'consigneeMobile', title: '收货手机'}
        ,{field: 'logisticsCompany', title: '物流公司'}
        ,{field: 'logisticsNum', title: '物流单号'}
        ,{field: 'createDate', title: '创建时间', templet :function (row){return createTime(row.createDate);}}
        ,{title: '操作', width: 250, align:'center', fixed: 'right', toolbar: '#table-useradmin-webuser'}
      ]]
      ,where: field
      ,method: 'POST'
      ,page: true
      ,limit: 10
      ,limits: [10,20,30,40,50,100,1000,5000,200000]
      ,height: 'full-320'
      ,text: '对不起，加载出现异常！'
      ,contentType: 'application/json'
      ,done:function(res, curr, count){  //res 接口返回的信息
        
        exportData=res.data;
        console.log(exportData);

        $("[data-field = 'disPrice']").children().each(function(){
          if($(this).text()!='优惠价'){
            $(this).text(($(this).text()/100).toFixed(2));
          }
        })
        $("[data-field = 'price']").children().each(function(){
          if( $(this).text()!='原价'){
            $(this).text(($(this).text()/100).toFixed(2));
          }
        })

        $("[data-field = 'totalPrice']").children().each(function(){
          if( $(this).text()!='总价'){
            $(this).text(($(this).text()/100).toFixed(2));
          }
        })
        $("[data-field = 'status']").children().each(function(){
          //0：未支付，1：已支付，2：待收货，3：已确认收货,5:全部
          if($(this).text() == '0'){
            $(this).text("未支付");
          }else if($(this).text() == '1'){
            $(this).text("已支付");
          }else if($(this).text() == '2'){
            $(this).text("待收货");
          }else if($(this).text() == '3'){
            $(this).text("已确认收货");
          }else if($(this).text() == '4'){
            $(this).text("订单取消");
          }
        })

        $("[data-field = 'gtype']").children().each(function(){
          //类别，1：升级套餐名称 ，2：商品名称
          if($(this).text() == '2'){
            $(this).text("升级套餐");
          }else if($(this).text() == '1'){
            $(this).text("普通商品");
          }
        })
      }
    });
  });

  //事件
  var active = {
    batchdel: function(){
      var checkStatus = table.checkStatus('LAY-order-manage')
      ,checkData = checkStatus.data; //得到选中的数据

      if(checkData.length === 0){
        return layer.msg('请选择数据');
      }
      
      layer.prompt({
        formType: 1
        ,title: '敏感操作，请验证口令'
      }, function(value, index){
        layer.close(index);
        
        layer.confirm('确定删除吗？', function(index) {
          
          //执行 Ajax 后重载
          /*
          admin.req({
            url: 'xxx'
            //,……
          });
          */
          table.reload('LAY-order-manage');
          layer.msg('已删除');
        });
      });
    }
    ,add: function(){
      admin.popup({
        title: '添加复消商品'
        ,area: ['1300px', '850px']
        ,id: 'LAY-popup-user-add'
        ,shade: false 
        ,success: function(layero, index){
          view(this.id).render('grade/userform').done(function(){
            form.render(null, 'layuiadmin-form-useradmin');
            
            //监听提交
            form.on('submit(LAY-user-front-submit)', function(data){
              var field = data.field; //获取提交的字段

              //提交 Ajax 成功后，关闭当前弹层并重载表格
              //$.ajax({});
              $.ajax({
                url: setter.myurl + "admin/grade/add",
                data: JSON.stringify(field),
                type: "POST",
                dataType: "json",
                contentType: "application/json",
                success: function(data){
                  if(data.code == 0){
                    layui.msg("复消商品添加成功");
                    layui.table.reload('LAY-grade-manage'); //重载表格
                    layer.close(index); //执行关闭 
                  }else{
                    layui.msg("复消商品添加失败");
                  }
                  
                }
              });
              // layui.table.reload('LAY-grade-manage'); //重载表格
              // layer.close(index); //执行关闭 
            });
          });
        }
      });
    }
  };
  
  $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
});
</script>